﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using CredentialsManager.Properties;
using MVVMFoundation;

namespace CredentialsManager.ViewModels
{
    public class ApplicationViewModel : ViewModelBase
    {
        #region Private member variables

        private string m_ApplicationName;        
        private ReadOnlyCollection<string> m_Applications;        
        private RelayCommand m_CreateCommand;

        #endregion

        #region Constructor

        public ApplicationViewModel(string displayName,
            IList<string> existingApplications)
        {
            this.DisplayName = displayName;

            if (existingApplications == null)
            {
                m_Applications = new ReadOnlyCollection<string>(new List<string>());
            }
            else
            {
                m_Applications = new ReadOnlyCollection<string>(existingApplications);
            }
        }

        public ApplicationViewModel(IList<string> existingApplications)
            : this(Resources.ApplicationViewModelDefaultDisplayName, existingApplications)
        { }

        #endregion

        #region Public properties

        public string ApplicationName
        {
            get
            {
                return m_ApplicationName;
            }
            set
            {
                m_ApplicationName = value;
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        public ReadOnlyCollection<string> Applications
        {
            get
            {
                return m_Applications;
            }
            private set
            {
                m_Applications = value;
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        #endregion

        #region Private methods

        private void Create()
        {
            Debug.Assert(!string.IsNullOrEmpty(this.ApplicationName) &&
                this.ApplicationName.Trim() != string.Empty);

            if (this.Applications.Contains(this.ApplicationName))
            {
                var errorMessageBox = base.GetService<IMessageBoxService>();
                Debug.Assert(errorMessageBox != null);

                errorMessageBox.Show(Resources.ApplicationAlreadyExistsErrorMessage,
                    this.DisplayName, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                var applications = new List<string>(this.Applications);
                applications.Add(this.ApplicationName);
                this.Applications = new ReadOnlyCollection<string>(applications);
                this.ApplicationName = string.Empty;
            }
        }

        #endregion

        #region Commands

        public ICommand CreateCommand
        {
            get
            {
                return m_CreateCommand ?? (m_CreateCommand = new RelayCommand(
                    param => this.Create()));
            }
        }

        #endregion
    }
}
